package de.tum.team26.eistpoll.client.UI;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.team26.eistpoll.client.EIST_POLL;

/**
 * Class to Display the already voted Panel
 */
public class AlreadyVoted extends UIElement {

	//UI Elements
	Label lblHeadding = new Label("Already Voted");
	Label lblText = new Label("Your IP has already committed a vote - thank you!");
	VerticalPanel alreadyVotedPanel = new VerticalPanel(); // Login Container
	Button btnSend = new Button("see results"); 
	HorizontalPanel headder = new HorizontalPanel();
	Label btnLogin = new Label("Login"); // Lecturer Login
	Image tum = new Image();
	Image info = new Image();
	Label lblErrMsg;
	
	// Controller
	EIST_POLL controller;
	
	/**
	 * Initialise the Panel
	 * @param controller
	 */
	public AlreadyVoted(EIST_POLL controller){
		
		this.controller = controller;
		
		btnLogin.addStyleName("btlogin");
		
		tum.setStyleName("tumlogo");
		info.setStyleName("infologo");
		tum.setUrl("tum.gif");
		info.setUrl("info.gif");
		headder.setStyleName("headder");
		// add UI Elements
		headder.add(btnLogin);
		headder.add(tum);
		headder.add(info);
		alreadyVotedPanel.add(headder);
		lblHeadding.addStyleName("h1");
		
		alreadyVotedPanel.add(lblHeadding);
		alreadyVotedPanel.add(lblText);
		btnSend.addStyleDependentName("submit");
		alreadyVotedPanel.add(btnSend);
		alreadyVotedPanel.addStyleName("container");
		// add click Handler
		btnLogin.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				triggerLogin();
			}
		});
		
		// add click Handler
		btnSend.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				triggerResults();
			}
		});
	}
	
	// As before
	private void triggerResults(){
		controller.triggerStatistic();
	}
	
	private void triggerLogin(){
		controller.triggerLogin();
	}
	
	@Override
	public VerticalPanel getContainer() {
		return alreadyVotedPanel;
	}

	@Override
	public void displayErrorMessage(String message) {
		if (lblErrMsg == null){
			lblErrMsg = new Label(message);
			lblErrMsg.setStyleName("error");
			alreadyVotedPanel.add(lblErrMsg);
		}
		else{
			lblErrMsg.setText(message);
		}
	}

}
